﻿Imports System.IO
Imports System.Net
Imports System.Windows.Forms.OpenFileDialog

Public Class Form1
    Public stropenFD As String
    Private TreeS As String
    Private _nullable As Control

    Private Property Nullable(ByVal Value1 As DBNull) As Control
        Get
            Return _nullable
        End Get
        Set(ByVal value As Control)
            _nullable = value
        End Set
    End Property

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        
        Dim i As Integer
        Me.TabPage1.Parent = Me.TabControl1
        i = TabControl1.TabPages.Count - 1
        TabControl1.SelectedIndex = i

    End Sub

    Private Sub listview2_size()

        Me.TreeView1.Left = 5
        Me.TreeView1.Width = Me.TabControl1.Width / 4
        Me.TreeView1.Height = Me.TabPage1.Height - 37

        Me.ListView1.Left = Me.TreeView1.Width + 10
        Me.ListView1.Width = Me.TreeView1.Width * 3
        Me.ListView1.Height = Me.TreeView1.Height

        Me.ListView2.Left = Me.TabControl1.Width / 4 * 3 + 15
        Me.ListView2.Width = Me.TabControl1.Width / 4
        Me.ListView2.Height = Me.ListView1.Height


        ' Me.ListView1.Width + 
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ListView2.Visible = False
        upyes.Visible = False
        upno.Visible = False


        getfptlist()
        listview2_size()
        Me.TabPage1.Parent = Me.TabControl1
        Me.TabPage2.Parent = Nullable(DBNull.Value)

    End Sub

    Private Sub ToolStripButton8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton8.Click
        Me.Close()
    End Sub

    Private Sub TreeView1_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs)
        MsgBox(e.Node.Text)
    End Sub



    '============================================== Upload ================================================
    '===========================================本地文件选择框=============================================
    
    '=================== OpenFileDialog 的实际使用  ================================
    Private Sub Button_UP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_UP.Click

        ListView2.Visible = True
        ListView2.Items.Clear()
        Me.ListView1.Width = Me.TabControl1.Width / 2
        upyes.Visible = True
        upno.Visible = True

        OpenFileDialog1.InitialDirectory = "c:\"
        OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
        OpenFileDialog1.FilterIndex = 2
        OpenFileDialog1.RestoreDirectory = True
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            MsgBox(OpenFileDialog1.FileName)
            stropenFD = OpenFileDialog1.FileName
        End If


        If OpenFileDialog1.SafeFileName() = "" Then
            MessageBox.Show("请重新选择文件！")
        Else
            Dim i As Integer
            For i = 0 To OpenFileDialog1.SafeFileNames().Length - 1
                Me.ListView2.Items.Add(OpenFileDialog1.SafeFileNames(i), 2)
            Next

        End If
    End Sub
    Private Sub upyes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles upyes.Click

        'Dim filepathUP As String = openFD()
        If FtpMod.Upload(LoginForm1.TextBox1.Text, LoginForm1.TextBox2.Text, "ftp://192.168.1.233/" & TreeS, stropenFD) = True Then
            'If FtpMod.Upload(LoginForm1.TextBox1.Text, LoginForm1.TextBox2.Text, "ftp://192.168.1.233/Class1/shipin/", "C:\works\upload123.txt") = True Then
            MessageBox.Show(stropenFD & "：上传成功！")
        Else

            MessageBox.Show("上传失败，请稍后重试！")
            ListView2.Visible = False
        End If

    End Sub
    '=================================================添加节点==========================================

    Class nn

        Public name As String

        Public parent As String

        Public snumber As String

    End Class
    Private Sub getfptlist()

        Dim nd(10) As nn

        Dim i As Integer

        For i = 0 To 9

            nd(i) = New nn
        Next

        nd(0).name = "CG英雄动漫教育" : nd(0).parent = "0" : nd(0).snumber = "1"

        nd(1).name = "一班" : nd(1).parent = "1" : nd(1).snumber = "2"

        nd(2).name = "二班" : nd(2).parent = "1" : nd(2).snumber = "3"

        nd(3).name = "视频" : nd(3).parent = "2" : nd(3).snumber = "4"

        nd(4).name = "素材" : nd(4).parent = "2" : nd(4).snumber = "5"

        nd(5).name = "作业" : nd(5).parent = "2" : nd(5).snumber = "6"

        nd(6).name = "视频" : nd(6).parent = "3" : nd(6).snumber = "7"

        nd(7).name = "素材" : nd(7).parent = "3" : nd(7).snumber = "8"

        nd(8).name = "作业" : nd(8).parent = "3" : nd(8).snumber = "9"
        Me.TreeView1.Nodes.Clear()



        For i = 0 To 8

            addto(nd(i), TreeView1.Nodes)

        Next

    End Sub


    Public Sub addto(ByVal nd As nn, ByVal nodes As TreeNodeCollection)

        Dim dd As TreeNode

        Dim tmpd As TreeNode

        If nd.parent = "0" Then

            tmpd = nodes.Add(nd.name)

            tmpd.ToolTipText = nd.snumber

            Exit Sub

        Else

            For Each dd In nodes

                If nd.parent = dd.ToolTipText Then

                    tmpd = dd.Nodes.Add(nd.name)

                    tmpd.ToolTipText = nd.snumber

                    Exit Sub

                End If

                addto(nd, dd.Nodes)

            Next

        End If

    End Sub

    '================================== =========节点选择========= =================================
    Private Function TreeView1_click(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        Dim N As TreeNode = CType(sender, Windows.Forms.TreeView).SelectedNode

        Dim stringlistnext As String()
        Dim S As String = N.Text
        '//如果树节点不为0，则显示提示信息， 
        Try

            If N.GetNodeCount(False) = 0 Then
                '//当前节点的文本字符 

                ListView1.Items.Clear()
                Dim i As Integer = 0

                Me.ListView1.LargeImageList = Me.ImageList2

                Me.ListView1.View = View.LargeIcon

                If N.Parent.Text = "一班" Then

                    If N.Text = "视频" Then
                        S = "Class1/shipin"
                        'S = "/shipin"
                        stringlistnext = FtpMod.GetFileList(LoginForm1.TextBox1.Text, LoginForm1.TextBox2.Text, _
                                                        "ftp://192.168.1.233/" & S & "/", "*.wmv")

                        For i = 0 To stringlistnext.Length - 1

                            Me.ListView1.Items.Add(stringlistnext(i), 1)

                        Next

                    ElseIf N.Text = "素材" Then
                        S = "Class1/sucai"
                        'S = "/sucai"
                        stringlistnext = FtpMod.GetFileList(LoginForm1.TextBox1.Text, LoginForm1.TextBox2.Text, _
                                                        "ftp://192.168.1.233/" & S & "/", "*.jpg")
                        For i = 0 To stringlistnext.Length - 1

                            Me.ListView1.Items.Add(stringlistnext(i), 0)

                        Next
                    ElseIf N.Text = "作业" Then
                        TreeS = "Class1/zuoye"
                        ' S = "/zuoye"
                        stringlistnext = FtpMod.GetFileList(LoginForm1.TextBox1.Text, LoginForm1.TextBox2.Text, _
                                                        "ftp://192.168.1.233/" & S & "/", "*.txt")
                        For i = 0 To stringlistnext.Length - 1

                            Me.ListView1.Items.Add(stringlistnext(i), 2)

                        Next
                    End If
                End If

                If N.Parent.Text = "二班" Then

                    If N.Text = "视频" Then
                        S = "Class2/shipin"
                        'S = "/shipin"
                        stringlistnext = FtpMod.GetFileList(LoginForm1.TextBox1.Text, LoginForm1.TextBox2.Text, _
                                                        "ftp://192.168.1.233/" & S & "/", "*.*")

                        For i = 0 To stringlistnext.Length - 1

                            Me.ListView1.Items.Add(stringlistnext(i), 1)

                        Next

                    ElseIf N.Text = "素材" Then
                        S = "Class2/sucai"
                        'S = "/sucai"
                        stringlistnext = FtpMod.GetFileList(LoginForm1.TextBox1.Text, LoginForm1.TextBox2.Text, _
                                                        "ftp://192.168.1.233/" & S & "/", "*.*")
                        For i = 0 To stringlistnext.Length - 1

                            Me.ListView1.Items.Add(stringlistnext(i), 0)

                        Next
                    ElseIf N.Text = "作业" Then
                        S = "Class2/zuoye"
                        ' S = "/zuoye"
                        stringlistnext = FtpMod.GetFileList(LoginForm1.TextBox1.Text, LoginForm1.TextBox2.Text, _
                                                        "ftp://192.168.1.233/" & S & "/", "*.*")
                        For i = 0 To stringlistnext.Length - 1

                            Me.ListView1.Items.Add(stringlistnext(i), 2)

                        Next
                    End If
                End If
            End If
            TreeS = S & "/"
        Catch ex As Exception

        End Try


    End Function

    '====================== listview中激活目标项后执行的事件=================================

    Private Sub ListView1_ItemActivate(ByVal sender As Object, ByVal e As EventArgs) _
     Handles ListView1.ItemActivate
        Me.TabPage2.Parent = Nullable(DBNull.Value)
        Me.TabPage2.Parent = Me.TabControl1

        AxWindowsMediaPlayer1.Width = TabPage2.Width
        AxWindowsMediaPlayer1.Height = TabPage2.Height - 37
        AxWindowsMediaPlayer1.Top = 37
        AxWindowsMediaPlayer1.Left = 0

        PictureBox2.Width = TabPage2.Width
        PictureBox2.Height = 37
        PictureBox2.Top = 0
        PictureBox2.Left = 0

        Dim i As Integer
        Me.TabPage2.Parent = Me.TabControl1
        i = TabControl1.TabPages.Count - 1
        TabControl1.SelectedIndex = i


        Dim x As Integer = 0

        Dim str As String = ""

        Dim s As String = ""
        For x = 0 To ListView1.SelectedItems.Count - 1

            s = ListView1.SelectedItems.Item(x).Text

            'str = str & "选中项索引=" & ListView1.SelectedItems.Item(x).Index.ToString & "；"

            'str = str & "在控件中的索引=" & ListView1.SelectedIndices.Item(x).ToString & vbCr

        Next

        'MessageBox.Show(s)

        AxWindowsMediaPlayer1.URL = "mms://TECHSERVER/VideoTest/" & s

    End Sub


    '======================================= 双击关闭当前 tabpages========================================


    Private Sub TabControl1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) _
        Handles TabControl1.DoubleClick
        Dim str As String
        str = TabControl1.SelectedTab.Name
        If str <> "TabPage1" Then
            If str = "TabPage2" Then
                Me.TabControl1.TabPages.Remove(Me.TabControl1.SelectedTab)
                AxWindowsMediaPlayer1.close()
                'AxWindowsMediaPlayer1.Dispose()
                'this.Close()

            End If
        End If

        'Me.TabControl1.TabPages.Remove(Me.TabControl1.SelectedTab)

    End Sub
    ''============================================测试用代码=============================
    'Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
    '    Dim i As Integer
    '    Me.TabPage2.Parent = Me.TabControl1
    '    i = TabControl1.TabPages.Count - 1
    '    TabControl1.SelectedIndex = i

    '    Me.TabPage2.Parent = Me.TabControl1

    'End Sub
    ''============================================================================================



    Private Sub Button_down_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_down.Click
        MessageBox.Show("文件下载成功！！！！")
    End Sub

    Private Sub upno_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles upno.Click
        ListView2.Visible = False
        upyes.Visible = False
        upno.Visible = False

        Me.ListView1.Width = Me.TabControl1.Width - Me.ListView1.Top
        'Me.ListView1.ContextMenu.FindMenuItem()
    End Sub

    
End Class
